package io.github.mymonstercat.utils.baidu;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.mymonstercat.tools.model.AiResponse;
import io.github.mymonstercat.tools.model.Chat;
import io.github.mymonstercat.tools.model.ContentInfo;
import io.github.mymonstercat.tools.text.TextParsingDispatcher;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
import java.io.*;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.model.chat.ChatResponse;

/**
 * 用户 业务层处理
 *
 * @author ruoyi
 */
@Service
@Slf4j
public class BaiduNLPUtils {

    private static final String AK = "ALTAKE4KqVPFL1aZNQ6j1xsxLn";
    private static final String SK = "7f16376af8da48b5aa134c78dffc08bc";

    public static AiResponse startAigcNew2(String content) throws IOException {

        ContentInfo contentInfo = new ContentInfo(content);
//        AiResponse aiResponse = aiService.startAigcNew2("{{content}}，请根据上述描写，生成 {{num}}条回答", contentInfo, 1);
//        List<String> data = aiResponse.getData();
//        data.forEach(item -> System.out.println(item));
//        return data.get(0).toString();

        // 使用安全认证AK/SK鉴权
        Qianfan qianfan = new Qianfan(AK, SK);

        // 指定模型并执行请求
        ChatResponse resp = qianfan.chatCompletion()
                .model("ERNIE-4.0-8K-Preview")
                .addMessage("user", contentInfo.getContent())
                .execute();

        String responseText = null;
        List<String> data = null;
        List<String> errors = new ArrayList<>();

        try {
            // 处理返回结果
            responseText = resp.getResult();

            // 使用文本解析器解析响应文本，获取数据列表
            data = TextParsingDispatcher.parse(responseText);
            if (data != null) {
                data = data.stream().distinct().collect(Collectors.toList());
            }
        } catch (Exception e) {
            log.error("Error processing response", e);
            errors.add(e.getMessage());
        }

        List<Chat> chatList = new ArrayList<>();

        // 创建并返回一个新的AI响应对象
        return new AiResponse(chatList, responseText, data, errors);
    }


    public static AiResponse textSimilar(String[] hwOCRTexts, String[] configTexts) throws IOException {

        String taskPromptTemplate = "Role: 票据识别专家\nBackground: 下面有两列字符串列表，字符串列表1是一张票据中识别的字符串列表，字符串列表2中的一个字符串代表一张票据模板的特征\nProfile: 你是一位票据的判断专家，擅长识别各种票据\nSkills: 票据解析、信息抽取、模式匹配、文本解析。\nGoals: 根据两个字符串列表，判断当前的票据是否相似于某个票据模板，并给出数值最高的相似度\nOutputFormat: similar为最高的相似度，explain为相应的解释。\n\n示例 JSON 结构\n```json\n{\n  'similar': '0.5',\n  'explain':'原因'\n}\n\n'''";

        String content = taskPromptTemplate + "#####字符串列表1开始####" + String.join(",", hwOCRTexts) + "######字符串列表1结束#######" +
        "#####字符串列表2开始####" + String.join(",", configTexts) + "######字符串列表2结束#######";

        ContentInfo contentInfo = new ContentInfo(content);
//        AiResponse aiResponse = aiService.startAigcNew2("{{content}}，请根据上述描写，生成 {{num}}条回答", contentInfo, 1);
//        List<String> data = aiResponse.getData();
//        data.forEach(item -> System.out.println(item));
//        return data.get(0).toString();

        // 使用安全认证AK/SK鉴权
        Qianfan qianfan = new Qianfan(AK, SK);

        // 指定模型并执行请求
        ChatResponse resp = qianfan.chatCompletion()
                .model("ERNIE-4.0-8K-Preview")
                .addMessage("user", contentInfo.getContent())
                .execute();

        String responseText = null;
        List<String> data = null;
        List<String> errors = new ArrayList<>();

        try {
            // 处理返回结果
            responseText = resp.getResult();

            // 使用文本解析器解析响应文本，获取数据列表
            data = TextParsingDispatcher.parse(responseText);
            if (data != null) {
                data = data.stream().distinct().collect(Collectors.toList());
            }
        } catch (Exception e) {
            log.error("Error processing response", e);
            errors.add(e.getMessage());
        }

        List<Chat> chatList = new ArrayList<>();

        // 创建并返回一个新的AI响应对象
        return new AiResponse(chatList, responseText, data, errors);
    }

    public static void main(String[] args) throws IOException {
//        BaiduNLPUtils baiduNLPUtils = new BaiduNLPUtils();
//        AiResponse aiResponse = baiduNLPUtils.startAigcNew2("你好吗", 1);
//        List<String> data = aiResponse.getData();
//        data.forEach(item -> System.out.println(item));
        System.out.println("xixi");
    }
}
